<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


/*******************
***  处理内部管理系统的数据
***  http://192.168.1.100:89/fshop/
*** 2017/05/10
**********/
class Fshop
{

 
	public $message_id = null;
   

    public function __construct()
    {
        $this->CI =& get_instance();
		$this->CI->load->database();
		$this->CI->load->model('Doit_m','S');
		$this->CI->load->model('Function_m','fun');
    }
	
	/*******************
	***  新品开发保存商品信息到Fshop
	***  http://192.168.1.100:89/fshop/
	*** 2017/06/26
	**********/	
	public function insertFshopProduct($id)
	{
		if($id)
		{
			$where=" AND id='".$id."'";
		}
		$rsdb = $this->CI->db->get_one("SELECT * FROM db_product_develop WHERE 1 $where AND status=6");
		if($rsdb)
		{
			$res = $this->CI->db->get_one("SELECT * FROM fshop.d_product WHERE productid='".$rsdb["sku"]."'");
			if($res)
			{
				return;
			}

			$overtime = date("Y-m-d H:i:s");
			if($rsdb["supplier"]==1)$factoryid =12;
			if($rsdb["supplier"]==2)$factoryid =10;

			$sql = "INSERT INTO fshop.d_product(productid,productname,productnameen,price,factoryprice,sellprice,categoryid,status,developer,developer1,qualityinfo,fabricinfo,remark,factoryid,createdate,createoper,oper,opertime,wages,source)VALUES('" . $rsdb["sku"] . "','" . $rsdb["name"] . "','".$rsdb["en_name"]."','" . $rsdb["samplecosprice"] . "','".$rsdb["bulkprice"]."','".$rsdb["saleprice"]."','',1,'0','0','".$rsdb["bodystandard"]."','".$rsdb["ck"]."','".$rsdb["samplememo"]."','".$factoryid."','".$rsdb["instime"]."','171',171,'".$overtime."','".$rsdb["wages"]."',2) ";
			
			$this->CI->db->query($sql);
			
			//存图片
			$rgoods = $this->CI->db->get_one("select * from db_goods where goods_sku = '".$rsdb["sku"]."'");
			$imglist = $this->CI->db->get_where("db_goods_images","goods_id='".$rgoods["goods_id"]."'")->result_array();
			if(count($imglist)>0)
			{
				foreach($imglist as $key=>$img)
				{
					$d = $key+1;
					$local_url = "http://192.168.1.66/".$img['local_url'];
					$sql = "INSERT INTO fshop.d_picture(productid,picurl,picttype,createdate,createoper)VALUES('" .$rsdb["sku"] . "','".$local_url."','复漫图片{$d}','".$overtime."',171)";
					$this->CI->db->query($sql);
				}

			}
			
		 }

		
		return true;
	}
	

	/*******************
	***  获取产品信息
	***  http://192.168.1.100:89/fshop/
	*** 2017/05/10
	**********/	
	public function getproduct($sku)
	{
		$res = $this->CI->db->get_where('fshop.d_product',"productid = '".$sku."'")->result_array();
		return $res;
	}
	
	/*******************
	***  处理产品信息
	***  只针对8位全数字的工厂SKU
	*** 2017/05/10
	**********/	
	public function batchproductlist($page)
	{
		//先处理图片信息
		$goodsnostr = "";
		$pageSize  =20;
		$sql = "SELECT goods_id,goods_sku FROM  db_goods WHERE 1";
        if ($page != null) {
            $offset = ($page - 1) * $pageSize;
            $limit = " LIMIT $pageSize OFFSET $offset";
        }
		
		$wheresql = $sql . $orderbys . $limit;
        $query = $this->CI->db->query($wheresql);
        $list = $query->result_array();
		if(count($list)==0)
		{
			echo $wheresql;
			echo "执行完成！";
			die;
		}
		foreach($list as $key=>$rs)
		{
			$rval = "";
			$goodsno = trim($rs["goods_sku"]);
			$goodsnostr.= $goodsno."<br>";
			$goods_id = $rs["goods_id"];
			if(is_numeric($goodsno) && preg_match('|^\d{8}$|',$goodsno))	//数字并且8位
			{
				if(substr($goodsno,0,2)==11 || substr($goodsno,0,2)==12)	//11、12开头的8位数字
				{
					$resgoods = $this->getproduct($goodsno);
					if($resgoods[0])
					{
						//$post["standard"] =   $resgoods[0]["qualityinfo"];	//质检信息
						//$post["fabricinfo"]  = $resgoods[0]["fabricinfo"];		//面料信息
						//$post["wages"]		 = $resgoods[0]["wages"];	//工时价

						/*
						if($resgoods[0]["price"]>0 && ($resgoods[0]["price"] != $rs["g_costprice"])){
							$post["g_costprice"]		 = $resgoods[0]["price"];	//成本价
							$g_costprice = $rs["g_costprice"]?$rs["g_costprice"]:0;
							$price = $resgoods[0]["price"];
							$rval.= "同步内部系统：<br>原成本->$g_costprice , 更新->$price<br>";
						
						}
						if($resgoods[0]["factoryprice"]>0 && ($resgoods[0]["factoryprice"] != $rs["bulkprice"])){
							$post["bulkprice"]		 = $resgoods[0]["factoryprice"];	//批量价
							$factoryprice = $resgoods[0]["factoryprice"];
							$bulkprice = $rs[bulkprice]?$rs[bulkprice]:0;
							$rval.= "  原关联价->$bulkprice , 更新->$factoryprice<br>";
						}
						if($resgoods[0]["sellprice"]>0  && ($resgoods[0]["sellprice"] != $rs["g_sellprice"])){
							$post["g_sellprice"]    =  $resgoods[0]["sellprice"];	//销售价
							$g_sellprice = $rs["g_sellprice"]?$rs["g_sellprice"]:0;
							$sellprice = $resgoods[0]["sellprice"];
							$rval.= "  原销售价->$g_sellprice , 更新->$sellprice<br>";
						}*/

						//$post["memo"]		 = $resgoods[0]["remark"];	//备注信息
					
						//更新质检信息面料等
						if($this->CI->db->update('db_goods',$post,"goods_sku='".$goodsno."'"))
						{
							if($rval)
							{
								$imgdata = "";
								$imgdata["goods_id"] = $goods_id;
								$imgdata["updatetime"] = date("Y-m-d H:i:s");
								$imgdata["createUid"] = 0;
								$imgdata["types"] = 1;
								$imgdata["content"] = $rval;
								$imgdata["createUser"] = "auto";
								$this->CI->db->query($this->CI->db->insert_string('db_goods_log', $imgdata));
							}
						}
					}

					$rsdb = $this->CI->db->get_one("SELECT * FROM db_goods_images  WHERE goods_id='".$goods_id."'");	//没有图片，抓取原来的图片
					if(!$rsdb)
					{
						$resgoodsImages = $this->CI->db->get_where('fshop.d_picture',"productid='".$goodsno."'")->result_array();
						if($resgoodsImages)
						{
							foreach($resgoodsImages as $key=>$rsImages)
							{
								$picurl =  $rsImages["picurl"];
								if($picurl)
								{
									$Images = $this->CI->S->getMabangImages($picurl,$goodsno);								//return false //没有找到图片
									if($Images){
										$postImages = "";
										$postImages["goods_id"] = $goods_id;
										$postImages["images_type"]  = 1;
										$postImages["filesize"]  = $Images["filesize"];
										$postImages["images_url"]  = $Images["images_url"];
										$postImages["local_url"]  = $Images["local_url"];
										$postImages["thumb_url"]  =  $Images["thumb_url"];
										$postImages["filename"]  = $Images["filename"];
										$postImages["createUser"]  = $_COOKIE["username"];
										$postImages["instime"]  = time();
										$this->CI->db->insert('db_goods_images',$postImages);
									}
								}
							}
						}
					}
				}
			}
		}
		
		return $goodsnostr;
	}

	/*******************
	***  获取8位和5位产品信息
	***  http://192.168.1.100:89/fshop/
	*** 2017/08/19
	**********/	
	public function getallproduct()
	{
		$newarr = $okarray = array();
		$res = $this->CI->db->get('fshop.d_linkproduct')->result_array();
		foreach($res as $key=>$rs)
		{
			$goods_sku = $rs["productid"];
			$goods = $this->CI->db->get_where("db_goods","goods_sku='".$goods_sku."'")->result_array();
			if(count($goods)==0)
			{
				$nonewarr[]=$goods_sku;
				continue;
			}
			if(count($goods)==1)
			{
				$resinfo = $this->CI->db->get_where('fshop.d_linkproductinfo',"linkproductid='".$rs['id']."'")->result_array();
				foreach($resinfo as $k=>$rsdb)
				{
					$subgoods_sku = $rsdb['mbsku'];
					$subgoods = $this->CI->fun->getdatabasesdetail("db_goods"," AND goods_sku='".trim($subgoods_sku)."'");
					if(!$subgoods)
					{
						
						$this->CI->load->library('Mabang');
						$Mb = $this->CI->mabang->getskuname(trim($subgoods_sku));	//获取马帮里面的数据
						if(!$Mb[0]->NAME)
						{
							$newarr[]=$subgoods_sku;
							continue;
						}
						
						$postdata ="";
						$postdata["goods_parentID"] = $goods[0]['goods_parentID'];
						$postdata['goods_parent_sku']	= $goods[0]['goods_parent_sku'];
						$postdata['goods_name']	= $Mb[0]->NAME;
						$postdata['yz']	= 0;
						$postdata['goods_sku']	= $subgoods_sku;
						$postdata['g_bigclassid']	= $goods[0]['g_bigclassid'];
						$postdata['g_secondclassid']=$goods[0]['g_secondclassid'];
						$postdata['g_threeclassid'] =$goods[0]['g_threeclassid'];
						$postdata['en_goods_name']	= $Mb[0]->NAMEEN;
						$postdata['goods_brand']	=null;
						$postdata['g_costprice']	= $goods[0]['g_costprice'];
						$postdata['g_procurement']  = 15;
						$postdata['g_inventory_day']= 15;
						$postdata['g_weight']		=$goods[0]['g_weight'];
						$postdata['g_sellprice']	=$goods[0]['g_sellprice'];
						$postdata['g_pd_length']	=  $Mb[0]->LENGTH;
						$postdata['g_pd_width']		= $Mb[0]->WIDTH;
						$postdata['g_pd_height']	= $Mb[0]->HIGH;
						$postdata['g_pd_declare_value']= $goods[0]['g_pd_declare_value'];
						$postdata['g_goodstypeID']	= 0;
						$postdata['g_sale_status']  = $goods[0]['g_sale_status'];
						$postdata['g_updatetime']	=$goods[0]['g_updatetime'];
						$postdata['g_checkman']	= '刘艺';
						$postdata['g_buyer']  = '刘艺';
						$postdata['g_createID']		=$_COOKIE["userid"];
						$postdata['g_createUser']	=$_COOKIE["username"];
						$postdata['standard']	=  $goods[0]['standard'];
						$postdata['memo']  = $goods[0]['memo'];

						$postdata['bulkprice']  = $goods[0]['bulkprice'];
						$postdata['wages']  = $goods[0]['wages'];
						$postdata['fabricinfo']  = $goods[0]['fabricinfo'];
						$postdata['g_supplier']=$goods[0]['g_supplier'];
						if($Mb[0]->ALIAS1)
						{
							$postdata['cognatelist']= $Mb[0]->ALIAS1;
						}
						$this->CI->db->query($this->CI->db->insert_string('db_goods', $postdata));
						$goods_id = $this->CI->db->insert_id();
						echo $goods_id;
						
						$imgdata = "";
						$imgdata["goods_id"] = $goods_id;
						$imgdata["updatetime"] = date("Y-m-d H:i:s");
						$imgdata["createUid"] = $_COOKIE["userid"];
						$imgdata["types"] = 1;
						$imgdata["content"] = '从马帮导入商品';
						$imgdata["createUser"] = $_COOKIE["username"];
						$this->CI->db->query($this->CI->db->insert_string('db_goods_log', $imgdata));
						
						
						$imgdata = "";
						$imgdata["goods_id"] = $goods_id;
						$imgdata["storage_name"] = '一号仓';
						$imgdata["storage_id"] = 1;
						$imgdata["stock"] =0;
						$imgdata["in_transit"] = 0;
						$imgdata["positions"] =$Mb[0]->LOCATION;
						$this->CI->db->query($this->CI->db->insert_string('db_goods_stock', $imgdata));
						
						//图片
						$goodsImages = $this->CI->db->get_where("db_goods_images","goods_id='".$goods[0]['goods_id']."'")->result_array();
						if(count($goodsImages)>0)
						{
							foreach($goodsImages as $k=>$img)
							{
								$postdata = "";
								$postdata["goods_id"] = $goods_id;
								$postdata["images_type"] =1;
								$postdata["images_url"] = $img['images_url'];
								$postdata["local_url"] = $img['local_url'];
								$postdata["thumb_url"] = $img['thumb_url'];
								$postdata["filename"] = $img['filename'];
								$postdata["filesize"] = $img['filesize'];
								$postdata["createUser"] = $img['createUser'];
								$postdata["instime"] = $img['instime'];
								$this->CI->db->query($this->CI->db->insert_string('db_goods_images', $postdata));
								
							}
						}
						
						
						$okarray[] = $subgoods_sku."->".$goods_sku."->".$Mb[0]->NAME;
						
						
						//echo $subgoods_sku."->".$goods_sku."->".$Mb[0]->NAME;
						//echo "<br>";
						
					}
				}
				
			}
		}
		"未导入SKU";
		print_r($newarr);
		echo "<hr><br>";
		echo "未导入SKU";
		print_r($nonewarr);
		echo "<hr><br>";
		print_r($okarray);


	}
	

	/*****查询获取尺码少的SKU***/
	function getMabangchima()
	{
		$newarray = array();
		$res = $this->CI->db->get_where('db_goods_parent',"length(p_parent_sku)>3")->result_array();
		foreach($res as $key=>$rs)
		{
			$this->CI->load->library('Mabang');
			$Mb = $this->CI->mabang->getskunamelike(trim($rs['p_parent_sku']),'SEQUENCEID,NAME,LOCATION,COSTPRICE');	//获取马帮里面的数据
			if($Mb[0]->SEQUENCEID)
			{
				foreach($Mb as $rd=>$goods)
				{
					$goodsst = $this->CI->db->get_where("db_goods","goods_sku='".$goods->SEQUENCEID."'")->result_array();
					if(count($goodsst)==0)
					{
						if($goods->SEQUENCEID){
						$newarray[$rd]["goods"] = $goods->SEQUENCEID;
						$newarray[$rd]["goods_parent_sku"] = $rs['p_parent_sku'];
						$newarray[$rd]["g_costprice"] = $goods->COSTPRICE;
						}
					}
				}
			}
		}
		$listdata="";
		$headers =array("商品编号","商品父编号","成本价");
		foreach($newarray as $key=>$rs){
			if($rs){
				$listdata[$key][] = $rs["goods"]; 
				$listdata[$key][] = $rs["goods_parent_sku"]; 
				$listdata[$key][] = $rs["g_costprice"]; 
			}
		}
		
		$this->CI->fun->ouputexclePro($headers,$listdata);
	
	}

	function getfumannewprodct()
	{
		$resskustr = $this->CI->db->get_where('fshop.d_newproductdeve',"status =8 and edittime >'2017-07-01 00:00:00' ")->result_array();
		if(count($resskustr)>0)
		{
			foreach($resskustr as $key=>$ressku)
			{
				$goods_sku = $ressku['sku'];
				$rsdb = $this->CI->db->get_one("SELECT * FROM db_goods WHERE goods_sku='".trim($goods_sku)."'");
				if(!$rsdb){
					
						$postdata="";
						$bigclassID = 2;
						$secondclassID =4;
						$ThreeclassID = 0;
						if($ressku["factoryid"] ==12)	//工厂1
						{
							$supplier = 1;
						}elseif($ressku["factoryid"] ==10)
						{
							$supplier = 2;
						}else{
							$supplier = 3;
						}
						
						if($ressku["edittime"]){
							$OPERTIME= $ressku["edittime"];
						}else{
							$OPERTIME="2016-01-01 23:59:59";
						}
						$postdata['p_parent_sku']	= $ressku["sku"];
						$postdata['p_parent_name']	= $ressku["skuname"];
						$postdata['p_en_goods_name']= $ressku["skunameen"];
						$postdata['p_yz']	= 0;
						$postdata['p_bigclassid']	= $bigclassID;
						$postdata['p_secondclassid']= $secondclassID;
						$postdata['p_threeclassid'] = $ThreeclassID;
						$postdata['p_goods_brand'] = null;
						$postdata['p_costprice']	= $ressku["factoryprice"]?$ressku["factoryprice"]:0;
						$postdata['p_procurement']  = 15;
						$postdata['p_inventory_day']= 15;
						$postdata['p_weight']		= 0;
						$postdata['p_sellprice']	= $ressku["saleprice"];
						$postdata['p_pd_length']	= 0;
						$postdata['p_pd_width']		= 0;
						$postdata['p_pd_height']	= 0;
						$postdata['p_pd_declare_value']= 60;
						$postdata['p_goodstypeID']	= 7;
						$postdata['p_sale_status'] =1;
						$postdata['p_updatetime']	=$OPERTIME;
						$postdata['p_intime']	=$OPERTIME;
						$postdata['p_createID']	=295;
						$postdata['p_createUser']='auto';
						$postdata['p_supplier']=$supplier;
						$postdata['p_checkman']	= '刘艺';
						$postdata['p_buyer']  = '刘艺';
						$postdata['memo']  = $ressku["remark"];		
						$postdata['wages']  = $ressku["wages"]?$ressku["wages"]:0;
						$postdata["bulkprice"] =$ressku["costpric"]?$ressku["costpric"]:0;
						$postdata["standard"] = $ressku['qualityinfo'];
						$postdata["fabricinfo"] = $ressku['fabricinfo'];
						
						$this->CI->db->trans_begin();
						
						$this->CI->db->query( $this->CI->db->insert_string('db_goods_parent', $postdata));
						$parentID = $this->CI->db->insert_id();
						
						/*********子商品**********/
					$postdata ="";
					$postdata["goods_parentID"] = $parentID;
					$parentList = $this->CI->db->get_where('db_goods_parent','p_parent_id='.$parentID)->result_array();
					$postdata['goods_parent_sku']	= $parentList[0]["p_parent_sku"];
					$postdata['goods_name']	= $ressku["skuname"];
					$postdata['yz']	= 0;
					$postdata['goods_sku']	= $ressku["sku"];
					$postdata['g_bigclassid']	= $parentList[0]['p_bigclassid'];
					$postdata['g_secondclassid']=$parentList[0]['p_secondclassid'];
					$postdata['g_threeclassid'] =$parentList[0]['p_threeclassid'];
					$postdata['en_goods_name']	=$ressku["skunameen"];
					$postdata['goods_brand']	=null;
					$postdata['g_costprice']	=$ressku["costpric"]?$ressku["costpric"]:0;
					$postdata['g_procurement']  = 15;
					$postdata['g_inventory_day']= 15;
					$postdata['g_weight']		=0;
					$postdata['g_sellprice']	=$ressku["saleprice"];
					$postdata['g_pd_length']	= 0;
					$postdata['g_pd_width']		=0;
					$postdata['g_pd_height']	= 0;
					$postdata['g_pd_declare_value']= 60;
					$postdata['g_goodstypeID']	= 0;
					$postdata['g_sale_status']  = $ressku["status"]?$ressku["status"]:1;
					$postdata['g_updatetime']	=$OPERTIME;
					$postdata['g_checkman']	= '刘艺';
					$postdata['g_buyer']  = '刘艺';
					$postdata['g_createID']		=0;
					$postdata['g_createUser']	='auto';
					//$postdata['standard']	=   $ressku["qualityinfo"];
					$postdata['memo']  = $ressku["remark"];	
					$postdata['g_supplier']=$supplier;
					$postdata['wages']  = $ressku["wages"]? $ressku["wages"]:0;	
					$postdata['fabricinfo']	= $ressku["fabricinfo"];
					$postdata["bulkprice"] =$ressku["factoryprice"]?$ressku["factoryprice"]:0;
					$postdata["standard"] = $ressku['qualityinfo'];

					$this->CI->db->query($this->CI->db->insert_string('db_goods', $postdata));
					$goods_id = $this->CI->db->insert_id();
					//更新日志
					$imgdata = "";
					$imgdata["goods_id"] = $goods_id;
					$imgdata["updatetime"] = date("Y-m-d H:i:s");
					$imgdata["createUid"] = 0;
					$imgdata["types"] = 1;
					$imgdata["content"] = '同步内部管理系统商品';
					$imgdata["createUser"] = 'auto';
					$this->CI->db->query($this->CI->db->insert_string('db_goods_log', $imgdata));
					//更新默认仓库
					$where = array('goods_id' => $goods_id);
					$rsdbstock = $this->CI->db->get_where('db_goods_stock',$where)->result_array();
					if(!$rsdbstock){
					$imgdata = "";
					$imgdata["goods_id"] = $goods_id;
					$imgdata["storage_name"] = '一号仓';
					$imgdata["storage_id"] = 1;
					$imgdata["stock"] =0;
					$imgdata["in_transit"] = 0;
					$imgdata["positions"] ='';
					$this->CI->db->query($this->CI->db->insert_string('db_goods_stock', $imgdata));
					}
					if ($this->CI->db->trans_status() === FALSE)
					{
						$this->CI->db->trans_rollback();
					}
					else
					{
						$this->CI->db->trans_commit();
					}
				
				}
			}
		}

	}

	//更新所有关联的尺码信息
	function getlinkproduct()
	{
		$res = $this->CI->db->get('fshop.d_linkproduct')->result_array();
		foreach($res as $key=>$rs)
		{
			$goods_sku = $rs["productid"];
			$goods = $this->CI->db->get_where("db_goods","goods_sku='".$goods_sku."'")->result_array();
			if(count($goods)==0)
			{
				continue;
			}
			if(count($goods)==1)
			{
				$linkproductID = $rs['id'];
				$goodslist= $this->CI->db->get_where('fshop.d_linkproductinfo',array("linkproductid"=>$linkproductID))->result_array();
				foreach($goodslist as $key=>$rd)
				{
					$mbsku = $rd['mbsku'];
					$size  = $rd['size'];
					$chima = $rd['measure'];
					$sex   = $rd['sex'];

					$erpgoods = $this->CI->db->get_where('db_goods',array('goods_sku'=>$mbsku))->result_array();
					if(count($erpgoods)>0)
					{
						$goods_id = $erpgoods[0]['goods_id'];
					
							$attr= $this->CI->db->get_where('db_goods_attr',array('goods_id'=>$goods_id))->result_array();
							
							if(count($attr)==0)
							{
								if($sex=='通用')
								{
									$attr_value = "通用";	
								}
								if($sex=='女')
								{
									$attr_value = "女性";	
								}
								if($sex=='男')
								{
									$attr_value = "男性";	
								}
								echo $this->CI->db->insert_string('db_goods_attr',array('goods_id'=>$goods_id,"attr_id"=>1,"attr_value"=>$attr_value)).";";
								echo "<br>";
								echo $this->CI->db->insert_string('db_goods_attr',array('goods_id'=>$goods_id,"attr_id"=>2,"attr_value"=>$chima)).";";
								echo "<br>";
								echo $this->CI->db->insert_string('db_goods_attr',array('goods_id'=>$goods_id,"attr_id"=>9,"attr_value"=>$size)).";";
								echo "<br>";
							}
						
					}
					
				}
				
			}
		}
		print_r($goodslist);
				echo $goods_sku;
				echo "<br>";
				die;
		die;
	}

}